from django.contrib.auth.models import User
from django.db import models

from common.utils import to_md5_hex


class User(models.Model):
    user_id = models.AutoField(primary_key=True, db_column='userid')
    username = models.CharField(unique=True, max_length=20)
    password = models.CharField(max_length=32)
    real_name = models.CharField(max_length=255, db_column='realname', blank=True, null=True)
    tel = models.CharField(max_length=11, blank=True, null=True)
    points = models.IntegerField(default=0)
    is_admin = models.BooleanField(db_column='isadmin', default=False, blank=True)

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        self.password = to_md5_hex(self.password)
        super(User, self).save()

    class Meta:
        # managed = False
        db_table = 'tb_user'


class LoginLog(models.Model):
    login_id = models.AutoField(primary_key=True, db_column='loginid')
    user = models.ForeignKey(User, models.DO_NOTHING, db_column='user_id')
    ip_address = models.CharField(max_length=255, db_column='ipaddr')
    log_date = models.DateTimeField(db_column='logdate')

    class Meta:
        # managed = False
        db_table = 'tb_login_log'


class Province(models.Model):
    prov_id = models.AutoField(primary_key=True, db_column='provid')
    prov_name = models.CharField(max_length=255, db_column='provname')

    class Meta:
        # managed = False
        db_table = 'tb_province'


class City(models.Model):
    city_id = models.AutoField(primary_key=True, db_column='cityid')
    city_name = models.CharField(max_length=255, db_column='cityname')
    prov = models.ForeignKey(Province, models.DO_NOTHING, db_column='prov_id')

    class Meta:
        # managed = False
        db_table = 'tb_city'


class District(models.Model):
    dist_id = models.AutoField(primary_key=True, db_column='distid')
    dist_name = models.CharField(max_length=255, db_column='distname')
    city = models.ForeignKey(City, models.DO_NOTHING, db_column='city_id')

    class Meta:
        # managed = False
        db_table = 'tb_district'


class HouseType(models.Model):
    type_id = models.IntegerField(primary_key=True, db_column='typeid')
    type_name = models.CharField(max_length=10, db_column='typename')

    class Meta:
        # managed = False
        db_table = 'tb_house_type'


class House(models.Model):
    house_id = models.AutoField(primary_key=True, db_column='houseid')
    area = models.IntegerField()
    contact_email = models.CharField(db_column='contacterEmail', max_length=50, blank=True, null=True)
    contact_name = models.CharField(db_column='contacterName', max_length=10)
    contact_qq = models.CharField(db_column='contacterQQ', max_length=12, blank=True, null=True)
    contact_tel = models.CharField(db_column='contacterTel', max_length=20)
    detail = models.CharField(max_length=200, blank=True, null=True)
    floor = models.IntegerField()
    main_photo = models.CharField(db_column='mainPhoto', max_length=255, blank=True, null=True)
    price = models.FloatField()
    pub_date = models.DateTimeField(db_column='pubDate', blank=True, null=True)
    street = models.CharField(max_length=255)
    title = models.CharField(max_length=50)
    total_floor = models.IntegerField(db_column='totalFloor', blank=True, null=True)
    district = models.ForeignKey(District, models.DO_NOTHING, blank=True, null=True)
    house_type = models.ForeignKey(HouseType, db_column='house_type_id', on_delete=models.DO_NOTHING, null=True)
    user = models.ForeignKey(User, db_column='user_id', on_delete=models.DO_NOTHING)

    class Meta:
        # managed = False
        db_table = 'tb_house'


class HousePhoto(models.Model):
    photo_id = models.AutoField(primary_key=True, db_column='photoid')
    photo_name = models.CharField(max_length=255, db_column='photoname')
    house = models.ForeignKey(House, db_column='house_id', on_delete=models.DO_NOTHING)

    class Meta:
        # managed = False
        db_table = 'tb_house_photo'
